package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyu
 * @data 2023/4/27 10:18
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public TurnoverReportVO getShopMoney(LocalDate begin, LocalDate end) {
        List<LocalDate> days = new ArrayList<>();
        //开始拿到指定时间段内具体每一天的日期
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            days.add(begin);
        }
        //开始计算每天的营业额
        List<Double> money = new ArrayList<>();
        for (LocalDate day : days) {
            //拿到每一天开始的时间
            LocalDateTime startEveryDay = LocalDateTime.of(day, LocalTime.MIN);
            //拿到每一天结束的时间
            LocalDateTime endEveryDay = LocalDateTime.of(day, LocalTime.MAX);
            Double moneySum = orderMapper.moneySum(startEveryDay, endEveryDay, Orders.COMPLETED);
            if (moneySum == null) {
                moneySum = 0.0;
            }
            money.add(moneySum);
        }

        return new TurnoverReportVO(StringUtil.join(",", days), StringUtil.join(",", money));
    }

    @Override
    public UserReportVO getUserCount(LocalDate begin, LocalDate end) {
        List<LocalDate> days = new ArrayList<>();
        while (!begin.equals(end)) {
            begin = begin.plusDays(+1);
            days.add(begin);
        }
        //用户总数集合
        List<Integer> userTotal = new ArrayList<>();
        //每日新增用户的集合
        List<Integer> everyDayAdd = new ArrayList<>();
        for (LocalDate day : days) {
            //拿到每天开始的时间
            LocalDateTime startEveryDay = LocalDateTime.of(day, LocalTime.MIN);
            //拿到每天结束的时间
            LocalDateTime endEveryDay = LocalDateTime.of(day, LocalTime.MAX);
            Integer everyAdd = orderMapper.getUserCount(startEveryDay, endEveryDay);
            everyDayAdd.add(everyAdd);
            Integer userCount = orderMapper.getUserCount(null, endEveryDay);
            userTotal.add(userCount);
        }
        return new UserReportVO(StringUtil.join(",", days),
                StringUtil.join(",", userTotal), StringUtil.join(",", everyDayAdd));
    }

    @Override
    public OrderReportVO getOrderData(LocalDate begin, LocalDate end) {
        //单独准备一份begins原数据,防止被覆盖
        LocalDate begins = begin;
        List<LocalDate> days = new ArrayList<>();
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            days.add(begin);
        }
        //每日订单的总数
        List<Integer> orderTotal = new ArrayList<>();
        //每日有效订单总数
        List<Integer> orderComplete = new ArrayList<>();
        for (LocalDate day : days) {
            LocalDateTime startEveryDay = LocalDateTime.of(day, LocalTime.MIN);
            LocalDateTime endEveryDay = LocalDateTime.of(day, LocalTime.MAX);
            //拿到每日订单总数
            Integer orderCount = orderMapper.getOrderCount(startEveryDay, endEveryDay, null);
            orderTotal.add(orderCount);
            //拿到每日有效订单总数
            Integer orderComp = orderMapper.getOrderCount(startEveryDay, endEveryDay, Orders.COMPLETED);
            orderComplete.add(orderComp);
        }
        LocalDateTime startDay = LocalDateTime.of(begins, LocalTime.MIN);
        LocalDateTime endDay = LocalDateTime.of(end, LocalTime.MAX);
        //拿到指定时间段所有订单的总数
        Integer orderAllCount = orderMapper.getOrderCount(startDay, endDay, null);
        //拿到指定时间段有效订单的总数
        Integer orderAllComp = orderMapper.getOrderCount(startDay, endDay, Orders.COMPLETED);
        //拿到指定时间段的订单完成率
        Double completeRate = 0.0;
        if (orderAllComp > 0) {
            completeRate = orderAllComp.doubleValue() / orderAllCount;
        }
        //开始封装数据
        return new OrderReportVO(StringUtil.join(",", days), StringUtil.join(",", orderTotal),
                StringUtil.join(",", orderComplete), orderAllCount, orderAllComp, completeRate);

    }

    @Override
    public SalesTop10ReportVO getTop10(LocalDate begin, LocalDate end) {
        LocalDateTime startDay = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endDay = LocalDateTime.of(end, LocalTime.MAX);
        List<GoodsSalesDTO> top10List = orderMapper.getTop10List(startDay, endDay);
        //商品名称集合
        List<String> goodsNames = new ArrayList<>();
        //商品销量集合
        List<Integer> goodsNumber = new ArrayList<>();
        //开始填写对应数据
        for (GoodsSalesDTO goodsSalesDTO : top10List) {
            goodsNames.add(goodsSalesDTO.getName());
            goodsNumber.add(goodsSalesDTO.getNumber());
        }
        return new SalesTop10ReportVO(StringUtil.join(",", goodsNames),
                StringUtil.join(",", goodsNumber));
    }
}
